package com.pbase.data.networkmanage;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkRequest;
import android.os.Build;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.Utils;
import com.pbase.tools.Logger;

import androidx.annotation.RequiresApi;

/**
 * Created by HP on 2018/1/11.
 * 网络连接管理
 * ConnectivityManager
 * 监控网络连接(Wi-Fi, GPRS, UMTS, etc.)状态
 * 提供监听器，以便界面及时感知，做出相应应对，若仅需在调用方法时判断，直接使用NetworkUtils工具类即可
 *
 */

public class NetWorkConnection {


    private Object callback;
    private Context context = Utils.getApp().getApplicationContext();
    private  NetWorkConnection.NetWorkConnectionListsener listsener;


    public void register() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
            context.registerReceiver(getReceiver(), filter);
        } else {
            ConnectivityManager connectivityManager =
                    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager != null)
                connectivityManager.registerNetworkCallback(new NetworkRequest.Builder().build(),
                        getNetworkCallback());
        }
    }

    public void unregister() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            context.unregisterReceiver(getReceiver());
        } else {
            ConnectivityManager connectivityManager =
                    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager != null)
                connectivityManager.unregisterNetworkCallback(getNetworkCallback());
        }
    }

    private BroadcastReceiver getReceiver() {
        if (callback == null) {
            callback = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    //获得网络连接服务
                    String action = intent.getAction();
                    Logger.d(getClass(), "action:" + action);
                    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
                        receive();
                    }
                }
            };
        }
        return (BroadcastReceiver) callback;
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private ConnectivityManager.NetworkCallback getNetworkCallback() {
        if (callback == null && Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            callback = new ConnectivityManager.NetworkCallback() {
                @Override
                public void onAvailable(Network network) {
                    super.onAvailable(network);
                    receive();
                }

                @Override
                public void onLost(Network network) {
                    super.onLost(network);
                    listsener.networkType(NetworkUtils.NetworkType.NETWORK_NO);
                }
            };
        }
        return (ConnectivityManager.NetworkCallback) callback;
    }

    private void receive() {
        if (!NetworkUtils.isConnected() ){
            listsener.networkType(NetworkUtils.NetworkType.NETWORK_NO);
        }else {
            listsener.networkType(NetworkUtils.getNetworkType() );
        }
    }



    public void setNetWorkConnectionListsener(
                                              NetWorkConnection.NetWorkConnectionListsener listsener) {
        this.listsener = listsener;

    }

    public interface NetWorkConnectionListsener {
          void  networkType(NetworkUtils.NetworkType networkType);

    }
}
